package com.ita.iosc.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.iosc.bean.Client;

/**
 * Servlet implementation class VerifyClientServlet
 */
public class VerifyClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VerifyClientServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Map<String, String> users = new HashMap<String, String>();
		users.put("Freedy", "123");
		String uname = request.getParameter("cname");
		String password = request.getParameter("password");
		if(uname.length() == 0 || password.length() == 0){
			if(uname.length() == 0 ){
				request.setAttribute("cnameError", "Invalid format of user name!");
			}
			if(password.length() == 0 ){
				request.setAttribute("passwordError", "Invalid format of user password!");
			}
			request.getRequestDispatcher("./login").forward(request, response);
		}else {			
			if(users.get(uname).equals(password)){
				HttpSession session = request.getSession();
				session.setAttribute("user", new Client(uname));
				response.sendRedirect("./listMerchant");
			}else {
				request.setAttribute("passwordError", "Password error!");
				request.getRequestDispatcher("./login").forward(request, response);
			}
		}
	}

}
